﻿Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Collections.Generic
Imports Microsoft.Office.Interop

'Imports Microsoft.Office.Interop.Outlook


Public Class InitialSyncForm
    Dim TotalTasksCounts As Integer
    Dim WithEvents mySync As SyncManager

    Delegate Sub EmptyDelegate()
    Delegate Sub OneIntDelegate(ByVal anInt As Integer)

    Private Sub InitialSyncForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.rbDuplicates.Checked = My.Settings.DuplicatesDuplicate
        Me.rbLatest.Checked = My.Settings.DuplicatesUseLatest
        Me.rbOutlook.Checked = My.Settings.DuplicatesUseOutlook
        Me.rbRTM.Checked = My.Settings.DuplicatesUseRTM
    End Sub



    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If Me.btnCancel.Text = "Close" Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
        Me.Close()
    End Sub

    Private Sub btnSync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSync.Click
        If rbDuplicates.Checked Or rbLatest.Checked Or rbOutlook.Checked Or rbRTM.Checked Then
            Me.btnSync.Enabled = False
            'btnCancel.Enabled = False
            Me.clbProgress.Visible = True
            Me.pnlOptions.Visible = False
            With ProgressBar1
                .Minimum = 0
                '.Maximum = TotalTasksCounts
                .Visible = True
            End With
            Me.Label2.Text = "Performing initial sync.  Please wait while the actions below are complete"
            'this is the only place were we do an initial/full sync
            mySync = SyncManager.StartFullSync
            My.Settings.DuplicatesDuplicate = Me.rbDuplicates.Checked
            My.Settings.DuplicatesUseLatest = Me.rbLatest.Checked
            My.Settings.DuplicatesUseOutlook = Me.rbOutlook.Checked
            My.Settings.DuplicatesUseRTM = Me.rbRTM.Checked
        Else
            MessageBox.Show("Please select a synchronization option above", "Sync Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Sub ReportProgress(ByVal count As Integer)
        Me.ProgressBar1.Value = count
    End Sub

#Region "Delegates"
    Sub OnAfterDataLoad(ByVal TaskCount As Integer)
        clbProgress.SetItemChecked(0, True)
        Me.ProgressBar1.Maximum = TaskCount
    End Sub
    Sub OnAfterSync()
        clbProgress.SetItemChecked(5, True)
        'enable UI
        btnCancel.Enabled = True
        btnCancel.Text = "Close"
        Me.ProgressBar1.Visible = False
    End Sub
    Sub OnAfterSyncDuplicateTasks()
        clbProgress.SetItemChecked(2, True)
    End Sub
    Sub OnAfterSyncOutlookOnlyTasks()
        Me.clbProgress.SetItemChecked(4, True)
    End Sub
    Sub OnAfterSyncRTMOnlyTasks()
        clbProgress.SetItemChecked(3, True)
    End Sub
    Sub OnBeforeSync()
        clbProgress.SetItemChecked(1, True)
    End Sub
#End Region

#Region "Event handlers"

    Private Sub mySync_AfterDataLoad(ByVal TaskCount As Integer) Handles mySync.AfterDataLoad
        Me.Invoke(New OneIntDelegate(AddressOf OnAfterDataLoad), TaskCount)
    End Sub

    Private Sub mySync_AfterSync() Handles mySync.AfterSync
        Me.Invoke(New EmptyDelegate(AddressOf OnAfterSync))
    End Sub

    Private Sub mySync_AfterSyncDuplicateTasks() Handles mySync.AfterSyncDuplicateTasks
        Me.Invoke(New EmptyDelegate(AddressOf OnAfterSyncDuplicateTasks))
    End Sub

    Private Sub mySync_AfterSyncOutlookOnlyTasks() Handles mySync.AfterSyncOutlookOnlyTasks
        Me.Invoke(New EmptyDelegate(AddressOf OnAfterSyncOutlookOnlyTasks))
    End Sub

    Private Sub mySync_AfterSyncRTMOnlyTasks() Handles mySync.AfterSyncRTMOnlyTasks
        Me.Invoke(New EmptyDelegate(AddressOf OnAfterSyncRTMOnlyTasks))
    End Sub


    Private Sub mySync_BeforeSync() Handles mySync.BeforeSync
        Me.Invoke(New EmptyDelegate(AddressOf OnBeforeSync))
    End Sub

    Private Sub mySync_BeforeSyncDuplicateTasks() Handles mySync.BeforeSyncDuplicateTasks

    End Sub

    Private Sub mySync_BeforeSyncOutlookOnlyTasks() Handles mySync.BeforeSyncOutlookOnlyTasks

    End Sub

    Private Sub mySync_BeforeSyncRTMOnlyTasks() Handles mySync.BeforeSyncRTMOnlyTasks

    End Sub

    Private Sub mySync_ReportProgress(ByVal Count As Integer) Handles mySync.ReportProgress
        Me.Invoke(New OneIntDelegate(AddressOf ReportProgress), Count)
    End Sub
#End Region

End Class
